from PyQt5.QtGui import QStandardItemModel, QStandardItem
from PyQt5.QtWidgets import QStyleFactory, QMessageBox
from common.common import Common


class ShowTabMotorCut:
    def __init__(self, ui, ptr):
        self.ui = ui
        self.ptr = ptr
        self.increase_model_control = None
        self.increase_model_show = None
        self.cut1_model_control = None
        self.cut1_model_show = None
        self.cut2_model_show = None
        self.cut2_model_control = None
        self.cut3_model_show = None
        self.cut3_model_control = None
        self.increase_model_reset = None
        self.cut1_model_reset = None
        self.cut2_model_reset = None
        self.cut3_model_reset = None

        pass

    def init_ui(self):
        self.init_tab_motor_group_increase()
        self.init_tab_motor_group_cut1()
        self.init_tab_motor_group_cut2()
        self.init_tab_motor_group_cut3()
        self.init_tab_motor_group_increase_reset()
        self.init_tab_motor_group_cut1_reset()
        self.init_tab_motor_group_cut2_reset()
        self.init_tab_motor_group_cut3_reset()
        self.init_connect()
        pass

    def init_connect(self):
        self.ui.pushButton_increase_control.clicked.connect(lambda: self.btn_increase_control_clicked("increase"))
        self.ui.pushButton_cut1_control.clicked.connect(lambda: self.btn_cut1_control_clicked("cut1"))
        self.ui.pushButton_cut2_control.clicked.connect(lambda: self.btn_cut2_control_clicked("cut2"))
        self.ui.pushButton_cut3_control.clicked.connect(lambda: self.btn_cut3_control_clicked("cut3"))
        self.ui.pushButton_increase_reset.clicked.connect(lambda: self.btn_increase_reset_clicked("increase reset"))
        self.ui.pushButton_cut1_reset.clicked.connect(lambda: self.btn_cut1_reset_clicked("cut1 reset"))
        self.ui.pushButton_cut2_reset.clicked.connect(lambda: self.btn_cut2_reset_clicked("cut2 reset"))
        self.ui.pushButton_cut3_reset.clicked.connect(lambda: self.btn_cut3_reset_clicked("cut3 reset"))

    def get_data_h_l(self, speed_max: int, speed_min: int, left_speed_str: str):
        dec_data = int(left_speed_str)
        if dec_data > speed_max or dec_data < speed_min:
            message = "输入值超出范围" + "["+ str(speed_min) + "," + str(speed_max) +"]"
            QMessageBox.warning(self.ptr, "Error", message, QMessageBox.Cancel)
            return
        h_data = Common.get_hex_data_h(dec_data)
        l_data = Common.get_hex_data_l(dec_data)
        return [h_data, l_data]

    def btn_increase_reset_clicked(self, info):
        print(info)
        increase_reset_str = self.increase_model_reset.item(0).child(0, 1).text()
        value_i = int(increase_reset_str)
        if value_i == 0:  # not reset
            Common().get_can_bus().send_data(0x224, [0xA5, 0x01, 0x38, 0x02, 0x00, 0x00, 0x00, 0xA5])
        elif value_i == 1:  # reset
            Common().get_can_bus().send_data(0x224, [0xA5, 0x01, 0x38, 0x02, 0x01, 0x00, 0x00, 0xA5])
        else:
            QMessageBox.warning(self.ptr, "Error", "非法输入值，只接受0-1", QMessageBox.Cancel)

    def btn_cut1_reset_clicked(self, info):
        print(info)
        if Common().get_can_bus() is None:
            QMessageBox.warning(self.ptr, "Error", "CAN没初始化，别试了", QMessageBox.Cancel)
            return
        cut1_reset_str = self.cut1_model_reset.item(0).child(0, 1).text()
        value_i = int(cut1_reset_str)
        if value_i == 0:  # not reset
            Common().get_can_bus().send_data(0x224, [0xA5, 0x01, 0x39, 0x02, 0x00, 0x00, 0x00, 0xA5])
        elif value_i == 1:  # reset
            Common().get_can_bus().send_data(0x224, [0xA5, 0x01, 0x39, 0x02, 0x01, 0x00, 0x00, 0xA5])
        else:
            QMessageBox.warning(self.ptr, "Error", "非法输入值，只接受0-1", QMessageBox.Cancel)

    def btn_cut2_reset_clicked(self, info):
        print(info)
        if Common().get_can_bus() is None:
            QMessageBox.warning(self.ptr, "Error", "CAN没初始化，别试了", QMessageBox.Cancel)
            return
        cut2_reset_str = self.cut2_model_reset.item(0).child(0, 1).text()
        value_i = int(cut2_reset_str)
        if value_i == 0:  # not reset
            Common().get_can_bus().send_data(0x224, [0xA5, 0x01, 0x3A, 0x02, 0x00, 0x00, 0x00, 0xA5])
        elif value_i == 1:  # reset
            Common().get_can_bus().send_data(0x224, [0xA5, 0x01, 0x3A, 0x02, 0x01, 0x00, 0x00, 0xA5])
        else:
            QMessageBox.warning(self.ptr, "Error", "非法输入值，只接受0-1", QMessageBox.Cancel)

    def btn_cut3_reset_clicked(self, info):
        print(info)
        if Common().get_can_bus() is None:
            QMessageBox.warning(self.ptr, "Error", "CAN没初始化，别试了", QMessageBox.Cancel)
            return
        cut3_reset_str = self.cut3_model_reset.item(0).child(0, 1).text()
        value_i = int(cut3_reset_str)
        if value_i == 0:  # not reset
            Common().get_can_bus().send_data(0x224, [0xA5, 0x01, 0x3B, 0x02, 0x00, 0x00, 0x00, 0xA5])
        elif value_i == 1:  # reset
            Common().get_can_bus().send_data(0x224, [0xA5, 0x01, 0x3B, 0x02, 0x01, 0x00, 0x00, 0xA5])
        else:
            QMessageBox.warning(self.ptr, "Error", "非法输入值，只接受0-1", QMessageBox.Cancel)

    def btn_increase_control_clicked(self, info):
        print(info)
        increase_speed_str = self.increase_model_control.item(0).child(0, 1).text()
        if not increase_speed_str.isdigit():
            QMessageBox.warning(self.ptr, "Error", "非法输入值，只接受整数值", QMessageBox.Cancel)
            return
        list_h_l = self.get_data_h_l(130, 0, increase_speed_str)
        if list_h_l is None:
            return
        if Common().get_can_bus() is None:
            QMessageBox.warning(self.ptr, "Error", "CAN没初始化，别试了", QMessageBox.Cancel)
            return
        Common().get_can_bus().send_data(0x224, [0xA5, 0x01, 0x23, 0x02, list_h_l[1], list_h_l[0], 0x00, 0xA5])
        pass

    def btn_cut1_control_clicked(self, info):
        print(info)
        cut1_speed_str = self.cut1_model_control.item(0).child(0, 1).text()
        list_h_l = self.get_data_h_l(9999, -9999, cut1_speed_str)
        if list_h_l is None:
            return
        Common().get_can_bus().send_data(0x224, [0xA5, 0x01, 0x24, 0x02, list_h_l[1], list_h_l[0], 0x00, 0xA5])

    def btn_cut2_control_clicked(self, info):
        print(info)
        cut2_speed_str = self.cut2_model_control.item(0).child(0, 1).text()
        list_h_l = self.get_data_h_l(9999, -9999, cut2_speed_str)
        if list_h_l is None:
            return
        Common().get_can_bus().send_data(0x224, [0xA5, 0x01, 0x25, 0x02, list_h_l[1], list_h_l[0], 0x00, 0xA5])

    def btn_cut3_control_clicked(self, info):
        print(info)
        cut3_speed_str = self.cut3_model_control.item(0).child(0, 1).text()
        list_h_l = self.get_data_h_l(9999, -9999, cut3_speed_str)
        if list_h_l is None:
            return
        Common().get_can_bus().send_data(0x224, [0xA5, 0x01, 0x26, 0x02, list_h_l[1], list_h_l[0], 0x00, 0xA5])
        pass

    @staticmethod
    def init_tree_increase_reset(model):
        item = QStandardItem('控制')
        model.appendRow(item)
        # model.setItem(0, 1, QStandardItem('[Object]'))
        # 添加子条目
        item_child = QStandardItem('调高复位(0复位)')
        item.appendRow(item_child)
        item.setChild(0, 1, QStandardItem(str(0)))
        pass

    def init_tab_motor_group_increase_reset(self):
        self.increase_model_reset = QStandardItemModel(self.ptr)
        self.increase_model_reset.setHorizontalHeaderLabels(['item', 'value'])

        self.init_tree_increase_reset(self.increase_model_reset)
        self.ui.treeView_increase_reset.setModel(self.increase_model_reset)
        # 调整第一列的宽度
        self.ui.treeView_increase_reset.header().resizeSection(0, 210)
        # 设置成有虚线连接的方式
        self.ui.treeView_increase_reset.setStyle(QStyleFactory.create('windows'))
        # 完全展开
        self.ui.treeView_increase_reset.expandAll()

    @staticmethod
    def init_tree_cut1_reset(model):
        item = QStandardItem('控制')
        model.appendRow(item)
        # model.setItem(0, 1, QStandardItem('[Object]'))
        # 添加子条目
        item_child = QStandardItem('切割1复位(0复位)')
        item.appendRow(item_child)
        item.setChild(0, 1, QStandardItem(str(0)))
        pass

    def init_tab_motor_group_cut1_reset(self):
        self.cut1_model_reset = QStandardItemModel(self.ptr)
        self.cut1_model_reset.setHorizontalHeaderLabels(['item', 'value'])

        self.init_tree_cut1_reset(self.cut1_model_reset)
        self.ui.treeView_cut1_reset.setModel(self.cut1_model_reset)
        # 调整第一列的宽度
        self.ui.treeView_cut1_reset.header().resizeSection(0, 220)
        # 设置成有虚线连接的方式
        self.ui.treeView_cut1_reset.setStyle(QStyleFactory.create('windows'))
        # 完全展开
        self.ui.treeView_cut1_reset.expandAll()

    @staticmethod
    def init_tree_cut2_reset(model):
        item = QStandardItem('控制')
        model.appendRow(item)
        # model.setItem(0, 1, QStandardItem('[Object]'))
        # 添加子条目
        item_child = QStandardItem('切割2复位(0复位)')
        item.appendRow(item_child)
        item.setChild(0, 1, QStandardItem(str(0)))
        pass

    def init_tab_motor_group_cut2_reset(self):
        self.cut2_model_reset = QStandardItemModel(self.ptr)
        self.cut2_model_reset.setHorizontalHeaderLabels(['item', 'value'])

        self.init_tree_cut2_reset(self.cut2_model_reset)
        self.ui.treeView_cut2_reset.setModel(self.cut2_model_reset)
        # 调整第一列的宽度
        self.ui.treeView_cut2_reset.header().resizeSection(0, 220)
        # 设置成有虚线连接的方式
        self.ui.treeView_cut2_reset.setStyle(QStyleFactory.create('windows'))
        # 完全展开
        self.ui.treeView_cut2_reset.expandAll()

    @staticmethod
    def init_tree_cut3_reset(model):
        item = QStandardItem('控制')
        model.appendRow(item)
        # model.setItem(0, 1, QStandardItem('[Object]'))
        # 添加子条目
        item_child = QStandardItem('切割3复位(0复位)')
        item.appendRow(item_child)
        item.setChild(0, 1, QStandardItem(str(0)))
        pass

    def init_tab_motor_group_cut3_reset(self):
        self.cut3_model_reset = QStandardItemModel(self.ptr)
        self.cut3_model_reset.setHorizontalHeaderLabels(['item', 'value'])

        self.init_tree_cut3_reset(self.cut3_model_reset)
        self.ui.treeView_cut3_reset.setModel(self.cut3_model_reset)
        # 调整第一列的宽度
        self.ui.treeView_cut3_reset.header().resizeSection(0, 220)
        # 设置成有虚线连接的方式
        self.ui.treeView_cut3_reset.setStyle(QStyleFactory.create('windows'))
        # 完全展开
        self.ui.treeView_cut3_reset.expandAll()

    @staticmethod
    def init_tree_increase_control(model):
        item = QStandardItem('控制')
        model.appendRow(item)
        # model.setItem(0, 1, QStandardItem('[Object]'))
        # 添加子条目
        item_child = QStandardItem('调高目标值mm')
        item.appendRow(item_child)
        item.setChild(0, 1, QStandardItem(str(0)))
        pass

    @staticmethod
    def init_tree_increase_show(model):
        item = QStandardItem('调高状态')
        model.appendRow(item)
        # model.setItem(0, 1, QStandardItem('[Object]'))
        # 添加子条目
        item_child = QStandardItem('调高位置mm')
        item.appendRow(item_child)
        item.setChild(0, 1, QStandardItem('[Object]'))

        # 添加子条目
        item_child = QStandardItem('调高位置霍尔数')
        item.appendRow(item_child)
        item.setChild(1, 1, QStandardItem('[Object]'))

        # # 添加子条目
        # item_child = QStandardItem('到位检测(初始位置)')
        # item.appendRow(item_child)
        # item.setChild(2, 1, QStandardItem('[Object]'))

        # 添加子条目
        item_child = QStandardItem('0位检测')
        item.appendRow(item_child)
        item.setChild(2, 1, QStandardItem('[Object]'))

        # # 添加子条目
        # item_child = QStandardItem('到位检测(霍尔2)')
        # item.appendRow(item_child)
        # item.setChild(4, 1, QStandardItem('[Object]'))
        pass

    def init_tab_motor_group_increase(self):
        self.increase_model_control = QStandardItemModel(self.ptr)
        self.increase_model_control.setHorizontalHeaderLabels(['item', 'value'])
        self.increase_model_show = QStandardItemModel(self.ptr)
        self.increase_model_show.setHorizontalHeaderLabels(['item', 'value'])
        self.init_tree_increase_control(self.increase_model_control)
        self.init_tree_increase_show(self.increase_model_show)
        self.ui.treeView_increase_control.setModel(self.increase_model_control)
        self.ui.treeView_increase_show.setModel(self.increase_model_show)
        # 调整第一列的宽度
        self.ui.treeView_increase_control.header().resizeSection(0, 200)
        self.ui.treeView_increase_show.header().resizeSection(0, 200)
        # 设置成有虚线连接的方式
        self.ui.treeView_increase_control.setStyle(QStyleFactory.create('windows'))
        self.ui.treeView_increase_show.setStyle(QStyleFactory.create('windows'))
        # 完全展开
        self.ui.treeView_increase_control.expandAll()
        self.ui.treeView_increase_show.expandAll()
        pass

    @staticmethod
    def init_tree_cut1_control(model):
        item = QStandardItem('控制')
        model.appendRow(item)
        # model.setItem(0, 1, QStandardItem('[Object]'))
        # 添加子条目
        item_child = QStandardItem('转速rpm(0-2000)')
        item.appendRow(item_child)
        item.setChild(0, 1, QStandardItem(str(0)))
        # 添加子条目
        # item_child = QStandardItem('转向(0/1)')
        # item.appendRow(item_child)
        # item.setChild(1, 1, QStandardItem(str(0)))
        # 添加子条目
        item_child = QStandardItem('运行周期s')
        item.appendRow(item_child)
        item.setChild(1, 1, QStandardItem(str(0)))
        item.child(1, 1).setEnabled(False)
        pass

    @staticmethod
    def init_tree_cut1_show(model):
        item = QStandardItem('切割电机1状态')
        model.appendRow(item)
        # model.setItem(0, 1, QStandardItem('[Object]'))

        # 添加子条目
        # item_child = QStandardItem('转向(0反1正)')
        # item.appendRow(item_child)
        # item.setChild(0, 1, QStandardItem('[Object]'))

        # 添加子条目
        item_child = QStandardItem('转速rpm')
        item.appendRow(item_child)
        item.setChild(0, 1, QStandardItem('[Object]'))

        # 添加子条目
        item_child = QStandardItem('电机电流mA')
        item.appendRow(item_child)
        item.setChild(1, 1, QStandardItem('[Object]'))

        # 添加子条目
        item_child = QStandardItem('扭矩')
        item.appendRow(item_child)
        item.setChild(2, 1, QStandardItem('[Object]'))

        # 添加子条目
        item_child = QStandardItem('错误代码')
        item.appendRow(item_child)
        item.setChild(3, 1, QStandardItem('[Object]'))
        pass

    def init_tab_motor_group_cut1(self):
        self.cut1_model_control = QStandardItemModel(self.ptr)
        self.cut1_model_control.setHorizontalHeaderLabels(['item', 'value'])
        self.cut1_model_show = QStandardItemModel(self.ptr)
        self.cut1_model_show.setHorizontalHeaderLabels(['item', 'value'])
        self.init_tree_cut1_control(self.cut1_model_control)
        self.init_tree_cut1_show(self.cut1_model_show)
        self.ui.treeView_cut1_control.setModel(self.cut1_model_control)
        self.ui.treeView_cut1_show.setModel(self.cut1_model_show)
        # 调整第一列的宽度
        self.ui.treeView_cut1_control.header().resizeSection(0, 180)
        self.ui.treeView_cut1_show.header().resizeSection(0, 180)
        # windows_style = QStyleFactory.create('windows')
        # 设置成有虚线连接的方式
        self.ui.treeView_cut1_control.setStyle(QStyleFactory.create('windows'))
        self.ui.treeView_cut1_show.setStyle(QStyleFactory.create('windows'))
        # 完全展开
        self.ui.treeView_cut1_control.expandAll()
        self.ui.treeView_cut1_show.expandAll()
        pass

    @staticmethod
    def init_tree_cut2_control(model):
        item = QStandardItem('控制')
        model.appendRow(item)
        # model.setItem(0, 1, QStandardItem('[Object]'))
        # 添加子条目
        item_child = QStandardItem('转速rpm(0-2000)')
        item.appendRow(item_child)
        item.setChild(0, 1, QStandardItem(str(0)))
        # # 添加子条目
        # item_child = QStandardItem('转向(0/1)')
        # item.appendRow(item_child)
        # item.setChild(1, 1, QStandardItem(str(0)))
        # 添加子条目
        item_child = QStandardItem('运行周期s')
        item.appendRow(item_child)
        item.setChild(1, 1, QStandardItem(str(0)))
        item.child(1, 1).setEnabled(False)

    @staticmethod
    def init_tree_cut2_show(model):
        item = QStandardItem('切割电机2状态')
        model.appendRow(item)
        # model.setItem(0, 1, QStandardItem('[Object]'))

        # 添加子条目
        # item_child = QStandardItem('转向(0反1正)')
        # item.appendRow(item_child)
        # item.setChild(0, 1, QStandardItem('[Object]'))

        # 添加子条目
        item_child = QStandardItem('转速rpm')
        item.appendRow(item_child)
        item.setChild(0, 1, QStandardItem('[Object]'))

        # 添加子条目
        item_child = QStandardItem('电机电流mA')
        item.appendRow(item_child)
        item.setChild(1, 1, QStandardItem('[Object]'))

        # 添加子条目
        item_child = QStandardItem('扭矩')
        item.appendRow(item_child)
        item.setChild(2, 1, QStandardItem('[Object]'))

        # 添加子条目
        item_child = QStandardItem('错误代码')
        item.appendRow(item_child)
        item.setChild(3, 1, QStandardItem('[Object]'))
        pass

    def init_tab_motor_group_cut2(self):
        self.cut2_model_control = QStandardItemModel(self.ptr)
        self.cut2_model_control.setHorizontalHeaderLabels(['item', 'value'])
        self.cut2_model_show = QStandardItemModel(self.ptr)
        self.cut2_model_show.setHorizontalHeaderLabels(['item', 'value'])
        self.init_tree_cut2_control(self.cut2_model_control)
        self.init_tree_cut2_show(self.cut2_model_show)
        self.ui.treeView_cut2_control.setModel(self.cut2_model_control)
        self.ui.treeView_cut2_show.setModel(self.cut2_model_show)
        # 调整第一列的宽度
        self.ui.treeView_cut2_control.header().resizeSection(0, 200)
        self.ui.treeView_cut2_show.header().resizeSection(0, 200)
        # windows_style = QStyleFactory.create('windows')
        # 设置成有虚线连接的方式
        self.ui.treeView_cut2_control.setStyle(QStyleFactory.create('windows'))
        self.ui.treeView_cut2_show.setStyle(QStyleFactory.create('windows'))
        # 完全展开
        self.ui.treeView_cut2_control.expandAll()
        self.ui.treeView_cut2_show.expandAll()
        pass

    @staticmethod
    def init_tree_cut3_control(model):
        item = QStandardItem('控制')
        model.appendRow(item)
        # model.setItem(0, 1, QStandardItem('[Object]'))
        # 添加子条目
        item_child = QStandardItem('转速rpm(0-2000)')
        item.appendRow(item_child)
        item.setChild(0, 1, QStandardItem(str(0)))
        # 添加子条目
        # item_child = QStandardItem('转向(0/1)')
        # item.appendRow(item_child)
        # item.setChild(1, 1, QStandardItem(str(0)))
        # 添加子条目
        item_child = QStandardItem('运行周期s')
        item.appendRow(item_child)
        item.setChild(1, 1, QStandardItem(str(0)))
        item.child(1, 1).setEnabled(False)

    @staticmethod
    def init_tree_cut3_show(model):
        item = QStandardItem('切割电机3状态')
        model.appendRow(item)
        # model.setItem(0, 1, QStandardItem('[Object]'))

        # 添加子条目
        # item_child = QStandardItem('转向(0反1正)')
        # item.appendRow(item_child)
        # item.setChild(0, 1, QStandardItem('[Object]'))

        # 添加子条目
        item_child = QStandardItem('转速rpm')
        item.appendRow(item_child)
        item.setChild(0, 1, QStandardItem('[Object]'))

        # 添加子条目
        item_child = QStandardItem('电机电流mA')
        item.appendRow(item_child)
        item.setChild(1, 1, QStandardItem('[Object]'))

        # 添加子条目
        item_child = QStandardItem('扭矩')
        item.appendRow(item_child)
        item.setChild(2, 1, QStandardItem('[Object]'))

        # 添加子条目
        item_child = QStandardItem('错误代码')
        item.appendRow(item_child)
        item.setChild(3, 1, QStandardItem('[Object]'))
        pass

    def init_tab_motor_group_cut3(self):
        self.cut3_model_control = QStandardItemModel(self.ptr)
        self.cut3_model_control.setHorizontalHeaderLabels(['item', 'value'])
        self.cut3_model_show = QStandardItemModel(self.ptr)
        self.cut3_model_show.setHorizontalHeaderLabels(['item', 'value'])
        self.init_tree_cut3_control(self.cut3_model_control)
        self.init_tree_cut3_show(self.cut3_model_show)
        self.ui.treeView_cut3_control.setModel(self.cut3_model_control)
        self.ui.treeView_cut3_show.setModel(self.cut3_model_show)
        # 调整第一列的宽度
        self.ui.treeView_cut3_control.header().resizeSection(0, 200)
        self.ui.treeView_cut3_show.header().resizeSection(0, 200)
        # windows_style = QStyleFactory.create('windows')
        # 设置成有虚线连接的方式
        self.ui.treeView_cut3_control.setStyle(QStyleFactory.create('windows'))
        self.ui.treeView_cut3_show.setStyle(QStyleFactory.create('windows'))
        # 完全展开
        self.ui.treeView_cut3_control.expandAll()
        self.ui.treeView_cut3_show.expandAll()
        pass

    # update UI component
    def update_increase(self, value, child_row, child_col):
        parent_item = self.increase_model_show.item(0)
        parent_item.child(child_row, child_col).setText(str(value))

    def update_cut1(self, value, child_row, child_col):
        parent_item = self.cut1_model_show.item(0)
        parent_item.child(child_row, child_col).setText(str(value))

    def update_cut2(self, value, child_row, child_col):
        parent_item = self.cut2_model_show.item(0)
        parent_item.child(child_row, child_col).setText(str(value))

    def update_cut3(self, value, child_row, child_col):
        parent_item = self.cut3_model_show.item(0)
        parent_item.child(child_row, child_col).setText(str(value))